import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {TabModal} from "@shared/components/tab/tab/tab.modal";
import {TrainClassDrillOutline, TrainClassroomScheme, TrainClassScheduleVO} from "@interface/index";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {PlanService} from "@train/plan/plan.service";

@Component({
  selector: 'seed-class-plan',
  templateUrl: './class-plan.component.html',
  styleUrls: ['./class-plan.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ParentClassPlanComponent implements OnInit {
  // 教学计划
  public tabs: Array<TabModal>;
  public classId: number;
  public teachScheme: TrainClassDrillOutline;
  public classSchedule: Array<TrainClassScheduleVO>;
  public defaultIcon = '';
  public className: string;

  constructor(
    public toolService: ToolsService,
    private router: Router,
    private _palnService: PlanService,
    private activatedRoute: ActivatedRoute,
  ) {
    this.tabs = [
      // {title: '学情分析', content: '暂无内容', tag: 'analysis', isShow: false, hasExtend: false, shortContent: ''},
      {title: '教学目标', content: '暂无内容', tag: 'objective', isShow: false, hasExtend: false, shortContent: ''},
      // {title: '教学措施', content: '暂无内容', tag: 'measure', isShow: false, hasExtend: false, shortContent: ''}
    ];
    // this.activatedRoute.params.subscribe((param: Params) => {
    //   this.classId = param['classId'];
    // });
    activatedRoute.params.switchMap((param: Params) => {
      this.classId = param['classId'];
      return activatedRoute.queryParams;
    }).subscribe((param: Params) => {
      this.className = param['className'];
    })
  }

  /*
* 切换tab 的显示全部与否
*/
  tabContentSwitch(i: number) {
    this.tabs[i].isShow = !this.tabs[i].isShow;
  }

  /**
   * @author ouxh862
   * @time 2018/1/31
   * @description 跳到教案详情
   * @param {TrainClassScheduleVO} schedule
   * @param {number} i
   */
  goToPlanDetail(schedule: TrainClassScheduleVO, i: number) {
    const params = {
      subjectName: this.toolService.encodeUri('课时' + (i + 1) + ':' + schedule.subjectName),
      teacherName: this.toolService.encodeUri(schedule.teacherName ? schedule.teacherName : '')
    };
    this.router.navigate([`/train/plan/detail/${schedule.id}`], {queryParams: params});
  }

  /**
   * @description
   * 根据班级ID获取 班级课时列表
   * @author @chenwh576
   * @time 2018.01.02
   * @param {number} classId
   * @return {Observable<SNSResult<TrainClassroomScheme>>}
   */
  fetchClassroomSchedule(classId: number): void {
    if (classId) {
      this._palnService.fetchClassroomSchedule(this.classId)
        .subscribe(res => {
          this.classSchedule = [...res.data];
        });
    }
  }

  /**
   * @description
   * 根据班级ID获取教学计划
   * @author @chenwh576
   * @time 2018.01.02
   * @param {number} classId
   */
  fetchTeachScheme(classId: number) {
    this._palnService.fetchDrilloutline(classId)
      .subscribe(res => {
        if(res.data){
          this.teachScheme = res.data;
          const keys = Object.keys(this.teachScheme);
          this.tabs.forEach((item, index) => {
            keys.map((objective, subindex) => {
              if (this.teachScheme[objective] !== '' && Object.is(objective, item['tag'])) {
                item.content = this.teachScheme[objective];
              }
            });
          });
          this.tabContentHandler();
        }
      });
  }

  /**
   * 判断 顶部tab 是否需要展开收起功能
   */
  tabContentHandler() {
    const tabs = this.tabs;
    for (let i = 0; i < 1; i++) {
      if (tabs[i].content.length > 110) {
        tabs[i].shortContent = tabs[i].content.slice(0, 111) + '...';
        tabs[i].hasExtend = true;
      }
    }
    this.tabs = tabs;
  }

  ngOnInit() {
    this.fetchTeachScheme(this.classId);
    this.fetchClassroomSchedule(this.classId);
  }

}
